/*
 * Copyright the original author or authors.
 * 
 * Licensed under the MOZILLA PUBLIC LICENSE, Version 1.1 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.mozilla.org/MPL/MPL-1.1.html
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package net.pixlib.sfe.view.editor 
{
	import net.pixlib.plugin.Plugin;
	import net.pixlib.sfe.event.LibraryStateEvent;
	import net.pixlib.sfe.util.getTranslation;
	import net.pixlib.sfe.view.ViewList;
	import net.pixlib.sfe.view.abstract.AsWingView;
	import net.pixlib.tms.LocalManager;
	import net.pixlib.tms.LocalManagerEvent;

	import org.aswing.BorderLayout;
	import org.aswing.Insets;
	import org.aswing.JLabel;
	import org.aswing.JMenuBar;
	import org.aswing.JPanel;
	import org.aswing.border.EmptyBorder;

	import flash.events.Event;
	import flash.filters.DropShadowFilter;

	/**
	 * Application status bar view.
	 * 
	 * @author Romain Ecarnot
	 */
	public class StatusbarView extends AsWingView 
	{
		//--------------------------------------------------------------------
		// Private properties
		//--------------------------------------------------------------------
		
		/** @private */
		private var _label : JLabel; 

		/** @private */
		private var _sDefaultName : String; 

		/** @private */
		private var _sFileName : String; 
		
		/** @private */
		private var _bFileNamed : Boolean;
		
		/** @private */ 		private var _bModified : Boolean; 

		
		//--------------------------------------------------------------------
		// Public API
		//--------------------------------------------------------------------
		
		/**
		 * Creates view.
		 */		
		public function StatusbarView(owner : Plugin)
		{
			super(owner, ViewList.STATUSBAR_VIEW, new JPanel(new BorderLayout(5,5)));
		}

		/**
		 * Triggered when library state has changed.
		 */
		public function onUpdateState(event : LibraryStateEvent) : void
		{
			_sFileName = event.path != null ?event.path : _sDefaultName;
			_bFileNamed = event.path != null;
			_bModified = event.modified;
			
			_onLocalChangeHandler();
		}

		
		//--------------------------------------------------------------------
		// Protected methods
		//--------------------------------------------------------------------
		
		/**
		 * Inits User Interface.
		 */
		override protected function onInitView() : void
		{
			LocalManager.getInstance().addEventListener(LocalManagerEvent.onLocalChangeEVENT, _onLocalChangeHandler);
			
			var m : JMenuBar = new JMenuBar();
			
			getComponent().setOpaque(true);
			getComponent().setBackground(m.getBackground());
			getComponent().filters = [new DropShadowFilter(1, 90, 0x000000, 0.35, 5, 5)];
			getComponent().setBorder(new EmptyBorder(null, new Insets(0, 0, 5, 0)));
			getComponent().setPreferredHeight(25);
			
			_label = new JLabel("", null, JLabel.LEFT);
			
			JPanel(getComponent()).append(_label, BorderLayout.CENTER);
			
			m = null;
			
			_bFileNamed = false;
			_bModified = false;
			
			_onLocalChangeHandler();
			 
			super.onInitView();
		}

		
		//--------------------------------------------------------------------
		// Private methods
		//--------------------------------------------------------------------
		
		/** @private */
		private function _onLocalChangeHandler(event : Event = null) : void
		{
			_sDefaultName = getTranslation("defaultFileName");
			
			_sFileName = _bFileNamed ? _sFileName : _sDefaultName;
			
			_label.setText(_sFileName + (_bModified ? " *" : ""));
			_label.repaintAndRevalidate();
		}
	}
}
